<template>
  <div>
    <el-card class="box-card">
      <el-form
        ref="queryFormRef"
        :model="queryInfo"
        @submit.native.prevent
        size="small"
        :inline="true"
      >
        <el-form-item label="姓名" prop="empName">
          <el-input
            v-model="queryInfo.empName"
            @keyup.enter.native="handleQuery"
            clearable
            placeholder="输入姓名"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button size="mini" type="primary" @click="handleQuery">查询</el-button>
          <el-button size="mini" @click="handleQueryInfoReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table
        ref="multipleTable"
        :data="employees"
        tooltip-effect="dark"
        style="width: 100%;"
        v-loading="loading"
        :header-cell-style="{ background: '#f8f8f9', color: '#515a6e' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" />
        <el-table-column label="头像">
          <template slot-scope="scope">
            <el-image
              style="width: 70px"
              :src="scope.row.empAvatar"
              :preview-src-list="[scope.row.empAvatar]"
              v-if="scope.row.empAvatar"
            ></el-image>
            <span v-else>{{'暂无头像'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="empName" label="真实姓名" />
        <el-table-column label="性别" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.row.sex === 1 ?'男': '女' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="nation" label="籍贯" show-overflow-tooltip width="120px" />
        <el-table-column prop="empPhone" label="手机号码" width="120px" />
        <el-table-column prop="email" label="邮箱" show-overflow-tooltip width="120px" />
        <el-table-column prop="graSchool" label="毕业院校" show-overflow-tooltip width="130px" />

        <el-table-column label="最高学位">
          <template slot-scope="{row}">
            <span>{{ ['无学位','学士','双学士','硕士','博士','博士后'][row.highestEdu - 1] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="speciality" label="专业" />
        <el-table-column label="人员来源">
          <template slot-scope="scope">
            <span>{{ ['校园招聘','社会招聘','其他'][scope.row.source - 1] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEntry(scope.row.id)" icon="el-icon-school">入职</el-button>
            <el-button type="text" @click="handleDel(scope.row.id)" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="getEmployees"
        @current-change="getEmployees"
        :current-page.sync="queryInfo.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 入职对话框 -->
    <el-dialog title="入职" :visible.sync="entryDialogVisable" width="600px">
      <el-card>
        <el-form
          :model="entryForm"
          :rules="rules"
          ref="entryFormRef"
          label-width="120px"
          size="small"
        >
          <el-form-item label="选择部门" prop="deptId">
            <dept-select :value.sync="entryForm.deptId" style="width:220px" />
          </el-form-item>
          <el-form-item label="选择岗位" prop="staId">
            <station-select :value.sync="entryForm.staId" />
          </el-form-item>
          <el-form-item label="入职日期" prop="empDate">
            <el-date-picker
              v-model="entryForm.empDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="入职日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="参加工作日期" prop="workDate">
            <el-date-picker
              v-model="entryForm.workDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="参加工作日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="用工形式" prop="empType">
            <el-radio-group v-model="entryForm.empType">
              <el-radio :label="true">正式员工</el-radio>
              <el-radio :label="false">临时员工</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否需要试用期" prop="isInternship">
            <el-radio-group v-model="entryForm.isInternship">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="试用期范围" v-if="entryForm.isInternship" prop="internshipDate">
            <el-date-picker
              v-model="entryForm.internshipDate"
              type="daterange"
              value-format="yyyy-MM-dd"
              class="date-range"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleReset('entryFormRef')">重置</el-button>
        <el-button type="primary" @click="handleSubmitEntry">确认入职</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getTalentPool, entryById, deleteTalent } from "@/api/employee";
export default {
  name: "talentPool",
  data() {
    return {
      queryInfo: {
        // 当前页
        pageNum: 1,
        // 页大小
        pageSize: 10,
        empName: undefined,
      },
      total: 0,
      employees: [],
      loading: false,
      entryDialogVisable: false,
      empId: undefined,
      entryForm: {
        deptId: undefined,
        staId: undefined,
        empDate: undefined,
        workDate: undefined,
        empType: undefined,
        isInternship: false,
        internshipDate: undefined,
      },
      rules: {
        deptId: [
          { required: true, message: "请选择入职部门", trigger: "blur" },
        ],
        staId: [{ required: true, message: "请选择入职岗位", trigger: "blur" }],
        empDate: [
          { required: true, message: "请选择入职日期", trigger: "blur" },
        ],
        workDate: [
          { required: true, message: "请选择工作日期", trigger: "blur" },
        ],
        empType: [
          { required: true, message: "请选择用工形式", trigger: "blur" },
        ],
        isInternship: [
          { required: true, message: "请选择是否试用", trigger: "blur" },
        ],
        internshipDate: [
          { required: true, message: "请选择试用日期范围", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getEmployees();
  },
  methods: {
    async getEmployees() {
      this.loading = true;
      let { code, data } = await getTalentPool(this.queryInfo);
      if (code === 200) {
        this.total = data.total;
        this.employees = data.employees;
      }
      this.loading = false;
    },
    handleQuery() {
      this.queryInfo.pageNum = 1;
      this.getEmployees();
    },
    handleReset(ref) {
      this.$refs[ref].resetFields();
    },
    handleQueryInfoReset() {
      this.handleReset("queryFormRef");
      this.getEmployees();
    },
    handleSelectionChange() {},
    handleEntry(id) {
      console.log(id);
      this.entryDialogVisable = true;
      this.empId = id;
    },
    handleSubmitEntry() {
      this.$refs.entryFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        } else {
          let id = this.empId;
          const { code } = await entryById(id, this.entryForm);
          if (code === 200) {
            this.$message("入职成功！");
            this.entryDialogVisable = false;
            this.getEmployees();
          }
        }
      });
    },
    handleDel(id) {
      this.$confirm("此操作将从人才库移除该员工, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let resp = await deleteTalent(id);
          if (resp.code === 200) {
            this.$message("删除成功！");
          }
          this.getEmployees();
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped lang="scss">
.el-pagination {
  margin: 18px 0px;
  float: right;
}
</style>